package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 老杨
 * @version 1.0
 */
@Service
public class CategoryServiceImpl implements CategoryService {

@Autowired
    CategoryMapper mapper;

@Autowired
    DishMapper dishMapper;

@Autowired
    SetmealMapper setmealMapper;


    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
       PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = mapper.page(categoryPageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }


    @Override
    public void add(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(0);
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        Long currentId = BaseContext.getCurrentId();
//        category.setCreateUser(currentId);
//        category.setUpdateUser(currentId);
        mapper.add(category);
    }

    @Override
    public void delete(Integer id) {
        //进行判断 该分类 下是否有 菜品
        Integer dish = dishMapper.haveDish(id);
        if(dish > 0){
           //有就不能删除
            throw  new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //判断是否关联 了套餐
        Integer integer = setmealMapper.haveSetmeal(id);
        if (integer>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        mapper.delete(id);
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());

        mapper.update(category);
    }

    @Override
    public void openordisable(Integer status, Long id) {
        Category build = Category.builder()
                .status(status)
                .id(id)
//                .updateTime(LocalDateTime.now())
//                .updateUser(BaseContext.getCurrentId())
                .build();
        mapper.update(build);
    }

    @Override
    public List<Category> list(Integer type) {
        List<Category> list = mapper.list(type);
        return list;
    }


}
